// 阶乘
function factorial(num) {
    let result = 1
    while (num) {
        result *= num
        num--
    }
    return result
}
// 排列

function arrange(total, size) {
    return factorial(total) / factorial(size) / factorial(total - size)
}

import $ from 'jquery'
class Base {
    /**
     * 初始化奖金玩法及说明
     * 
     * @memberof Base
     */
    initPlayList() {
        this.play_list.set('r2', {
                bonus: 6,
                tip: '11选2',
                name: '任二'
            })
            .set('r3', {
                bonus: 19,
                tip: '11选3',
                name: '任三'
            })
            .set('r4', {
                bonus: 78,
                tip: '11选4',
                name: '任四'
            })
            .set('r5', {
                bonus: 540,
                tip: '11选5',
                name: '任五'
            })
            .set('r6', {
                bonus: 90,
                tip: '11选6',
                name: '任二'
            })
            .set('r7', {
                bonus: 26,
                tip: '11选7',
                name: '任二'
            })
            .set('r8', {
                bonus: 9,
                tip: '11选8',
                name: '任二'
            })
    }

    /**
     * 初始化号码
     * 
     * @memberof Base
     */
    initNumber(){
        for(let i = 1;i<12;i++){
            this.number.add((''+i).padStart(2, '0'))
        }
    }

    /**
     * 设置遗漏数据
     * 
     * @param {any} omit 
     * @memberof Base
     */
    setOmit(omit){
        let  _this = this
        _this.omit.clear()
        for(let [index,item] of omit.entries()){
            _this.omit.set(index, item)
        }
        $(_this.omit_el).each(function(index,item) {
            $(item).text(_this.omit.get(index))
        })
    }

    /**
     * 设置开奖
     * 
     * @param {any} code 
     * @memberof Base
     */
    setOpenCode(code){
        let _this = this
        _this.open_code.clear()
        for(let item of code.values()){
            _this.open_code.add(item)
        }
        _this.updateOpenCode && _this.updateOpenCode.call(_this, code)
    }

/**
 * 切换选择状态
 * 
 * @param {any} e 
 * @memberof Base
 */
toggleCodeActive(e){
    let _this = this
    let $cur = $(e.currentTarget)
    $cur.toggleClass('btn-boll-active')
    _this.getCount()
    }

/**
 * 切换玩法
 * 
 * @param {any} e 
 * @memberof Base
 */
changePlayNav(e){
    let _this = this
    let $cur = $(e.currentTarget)
    $cur.addClass('active').siblings().removeClass('active')
    _this.cur_play = $cur.attr('desc').toLocaleLowerCase()
    $('#zx_sm span').html(_this.play_list.get(_this.cur_play).tip)
    $('.boll-list .btn-boll').removeClass('btn-boll-active')
    _this.getCount()
    }

    /**
     * 操作区奇数偶数等
     * 
     * @param {any} e 
     * @memberof Base
     */
    assistHandle(e){
        e.preventDefault();
        let _this = this
        let $cur = $(e.currentTarget)
        let index = $cur.index()
        $('.boll-list .btn-boll').removeClass('btn-boll-active')
        if(index === 0){
            $('.boll-list .btn-boll').addClass('btn-boll-active')

        }
        if(index === 1){
            $('.boll-list .btn-boll').each(function(i,t) {
                if(t.textContent>5){
                    $(t).addClass('btn-boll-active')
                }
            })
        }
        if(index === 2){
            $('.boll-list .btn-boll').each(function (i, t) {
                if (t.textContent <= 5) {
                    $(t).addClass('btn-boll-active')
                }
            })
        }
        if (index === 3) {
            $('.boll-list .btn-boll').each(function (i, t) {
                if (t.textContent % 2 === 1) {
                    $(t).addClass('btn-boll-active')
                }
            })
        }
        if (index === 4) {
            $('.boll-list .btn-boll').each(function (i, t) {
                if (t.textContent % 2 === 0) {
                    $(t).addClass('btn-boll-active')
                }
            })
        }
        _this.getCount()
    }

    /**
     * 获取当前彩票种类
     * 
     * @returns 
     * @memberof Base
     */
    getName(){
        return this.name
    }


    /**
     * 添加方法
     * 
     * @memberof Base
     */
    addCode(){
        let _this = this
        let $active = $('.boll-list .btn-boll-active').text().match(/\d{2}/g)
        let active = $active?$active.length:0
        let count = _this.computeCount(active, _this.cur_play)
        if(count){
            _this.addCodeItem($active.join(' '), _this.cur_play, _this.play_list.get(_this.cur_play).name,count)
        }
    }

    /**
     * 添加单次号码
     * 
     * @param {any} code 
     * @param {any} type 
     * @param {any} typeName 
     * @param {any} count 
     * @memberof Base
     */
    addCodeItem(code, type, typeName, count){
        let _this = this
        const tpl = `
        <li codes = "${type}|${code}" bonus="${count*2}" count="${count}">
            <div class="code">
                <b>${typeName}${count>1?"复试":"单式"}</b>
                <b class="em">${code}</b>
                [${count}注，<em class="code-list-money">${count*2}</em>元]
            </div>
        </li>
        `
        $(_this.cart_el).append(tpl)
        _this.getTotal()
    }


    getCount(){
        let _this = this
        let active = $('.boll-list .btn-boll-active').length
        let count = _this.computeCount(active, _this.cur_play)
        let range = _this.computeBonus(active, _this.cur_play)
        
        let money = count * 2
        let win1 = range[0]-money
        let win2 = range[1]-money
        let tpl
        let c1 = (win1<0&&win2<0)?Math.abs(win1):win1
        let c2 = (win1<0&&win2<0)?Math.abs(win2):win2
        if(count === 0){
            tpl = `您选择了<b class="red">${count}</b>注，共 <b class="red">${count*2}</b> 元`
        }else if(range[0]===range[1]){
            tpl = `您选择了<b>${count}</b>注，共<b>${count*2}</b>元 <em>若中奖， 奖金：<strong class="red">${range[0]}</strong>元，
            您将${win1>=0?"盈利":"亏损"}
            <strong class='${win1>=0?"red":"green"}'>${Math.abs(win1)}</strong>元
            </em>`
        }else{
            tpl = `
                您选择了<b>${count}</b>注，共<b>${count*2}</b>元 <em>若中奖， 奖金：<strong class="red">${range[0]}</strong>
                至<strong class="red">${range[1]}</strong>元，
                您将${win1< 0&&win2<0 ? "亏损" : "盈利"}
            <strong class='${win1 >= 0 ? "red" : "green"}'>${c1}</strong>至
            <strong class='${win2 >= 0 ? "red" : "green"}'>${c1}</strong>元
                </em>
            `
        }
        $('.sel_info').html(tpl)
    }

    
    
    /**
     * 计算金额
     * 
     * @memberof Base
     */
    getTotal(){
        let count = 0
        $('.codelist li').each(function (index, item) {
            count+= $(item).attr('count')*1
        })
        $('#count').text(count)
        $('#money').text(count*2)
    }

    /**
     * 随机数
     * 
     * @param {any} num 
     * @memberof Base
     */
    getRandom(num){
        let arr = [],index
        let number= Array.from(this.number)
        while(num--){
            index = Number.parseInt(Math.random()*number.length)
            arr.push(number[index])
            number.splice(index,1)
        }
        return arr.join(' ')
    }

    /**
     * 添加随机号码
     * 
     * @param {any} e 
     * @memberof Base
     */
    getRandomCode(e){
        e.preventDefault();
        let num = e.currentTarget.getAttribute('count')
        let play = this.cur_play.match(/\d+/g)[0]
        let _this = this
        if(num ==='0'){
            $(_this.cart_el).html('')
        }else{
            for(let i =0; i<num; i++){
                _this.addCodeItem(_this.getRandom(play),_this.cur_play,_this.play_list.get(_this.cur_play).name, 1)
            }
        }
    }
}
export {
    Base,
    arrange
}